package com.example.springaop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-24
 * Time: 17:28
 */
@Slf4j
@Aspect
//@Component
public class AspectDemo {
    //把切点表达式提取出来，作为公共部分，使用注解@Pointcut
    @Pointcut("execution(* com.example.springaop.controller.*.*(..))")
    public void pt(){}
//    前置通知
    @Before("pt()")
    public void doBefore(){
        log.info("执行Before方法");
    }
//    后置通知
    @After("pt()")
    public void doAfter(){
        log.info("执行After方法");
    }

//    返回后通知
    @AfterReturning("pt()")
    public void doAfterReturning(){
        log.info("执行AfterReturning方法");
    }
//    抛出异常后通知
    @AfterThrowing("pt()")
    public void doAfterThrowing(){
        log.info("执行AfterThrowing方法");
    }

    @Around("pt()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("Around方法开始执行");
        Object result = joinPoint.proceed();
        log.info("Around方法结束执行");
        return result;
    }


}
